import 'dart:convert';

import 'package:fluro/fluro.dart';
import 'package:flutter_1800/base/pictureOverview.dart';
import 'package:flutter_1800/pages/goods/GoodsDetail.dart';
import 'package:flutter_1800/pages/home.dart';
import 'package:flutter_1800/pages/user/findPassword.dart';
import 'package:flutter_1800/pages/user/loginView.dart';
import 'package:flutter_1800/pages/user/registerView.dart';
import 'package:flutter_1800/pages/user/settingView.dart';
import 'package:flutter_1800/pages/user/userCenterView.dart';
import 'package:flutter_1800/pages/web/linkWebview.dart';

class Routers {
  // 登录页面
  static final Handler _loginHandler =
      Handler(handlerFunc: (context, parameters) {
    return const LoginViewPage();
  });

  // 设备列表
  static final Handler _registerHandler =
      Handler(handlerFunc: (context, parameters) {
    return const RegisterViewPage();
  });

  // 设备列表
  static final Handler _homeHandler =
      Handler(handlerFunc: (context, parameters) {
    return const HomePage();
  });

  // 设备列表
  static final Handler _userCenterHandler =
      Handler(handlerFunc: (context, parameters) {
    String? id = parameters['id']?.first;
    return UserCenterViewPage(id: id);
  });

  // 访问webview列表
  static final Handler _linkPageHandler =
      Handler(handlerFunc: (context, parameters) {
    String? url = parameters['url']?.first;

    bool? landscape = (parameters['landscape']?.first ?? "") == "1";

    return LinkWebviewPage(url: url, landscape: landscape);
  });

  // 设备列表
  static final Handler _settingHandler =
      Handler(handlerFunc: (context, parameters) {
    return const Setting();
  });

  /// fluro 传递后取出参数，解析
  static String fluroCnParamsDecode(String encodeCn) {
    List<int> list = [];

    ///字符串解码
    for (var data in jsonDecode(encodeCn)[0]) {
      list.add(data);
    }
    return const Utf8Decoder().convert(list);
  }

  static final Handler _pictureHandler =
      Handler(handlerFunc: (context, parameters) {
    String? image = parameters['image']?.first;

    return PictureOverview(image: image);
  });

  static final Handler _goodsDetailHandler =
      Handler(handlerFunc: (context, parameters) {
    String? id = parameters['id']?.first;
    return GoodsDetail(int.parse(id!));
  });

  static final Handler _findPasswordHandler =
      Handler(handlerFunc: (context, parameters) {
    return const FindPassword();
  });

  static void configureRoutes(FluroRouter router) {
    // 登录页面
    router.define('/', handler: _homeHandler);
    // 登录页面
    router.define('/login', handler: _loginHandler);
    // 注册
    router.define('/register', handler: _registerHandler);
    router.define('/linkWeb', handler: _linkPageHandler);
    router.define('/setting', handler: _settingHandler);
    router.define('/userCenter', handler: _userCenterHandler);
    router.define('/pictureOverview', handler: _pictureHandler);
    router.define('/goodsDetail', handler: _goodsDetailHandler);
    router.define('/findPassword', handler: _findPasswordHandler);
  }
}
